package com.github.aloxc.plugin.restplus.component;

import com.github.aloxc.plugin.restplus.common.Consts;
import com.github.aloxc.plugin.restplus.entity.Group;
import com.github.aloxc.plugin.restplus.method.HttpMethod;
import com.github.aloxc.plugin.restplus.navigation.action.RestServiceItem;

import javax.swing.*;
import javax.swing.tree.DefaultTreeCellRenderer;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
 *
 * @author leero
 */
public class ServiceTreeNodeCellRenderer extends DefaultTreeCellRenderer {
    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
        if (value instanceof ServiceNode) {
            ServiceNode serviceNode = (ServiceNode) value;
            RestServiceItem serviceItem = serviceNode.getRestServiceItem();
            Icon icon = Consts.Method.get(serviceItem.getMethod(), serviceItem.isFoundRequestBody());
            if (icon != null) {
                this.setIcon(icon);
            }

            setText(serviceItem.getUrl());
            setToolTipText(serviceItem.getDescription());
        } else if (value instanceof ServiceRootNode) {
            ServiceRootNode rootNode = (ServiceRootNode) value;
            Icon icon = Consts.Icons.MODULE;
            this.setIcon(icon);
            setText(String.format(rootNode.isSwagger() ? "Swagger✖%d" : "Service✖%d", rootNode.getCount()));
        } else if (value instanceof ServiceModuleNode) {
            ServiceModuleNode moduleNode = (ServiceModuleNode) value;
            Icon icon = null;
            if(moduleNode.isSwagger()){
                icon = Consts.Icons.MODULE_NODE;
            }else {
                icon = Group.get() == Group.METHOD ?
                        Consts.Method.get(HttpMethod.getByRequestMethod(moduleNode.getRestServiceProject().getName()), false) : Consts.Icons.MODULE_NODE;
            }
            this.setIcon(icon);
            String format = "%s✖%d";
            setText(String.format(format, moduleNode.getRestServiceProject().getName(), moduleNode.getCount()));
        }
        return this;
    }
}
